package com.rykj.energy.database.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rykj.energy.database.common.EqConditions;
import com.rykj.energy.database.pojo.BasePojo;
import com.rykj.energy.database.service.IBaseService;

import java.util.List;

/**
 * @author GanLu
 * @date 2020-04-16 15:47
 */
public class BaseService<M extends BaseMapper<T>, T extends BasePojo> extends ServiceImpl<M, T> implements IBaseService<T> {


    @Override
    public T getOneByProperty(SFunction<T,?> property, Object value){
        return lambdaQuery().eq(property,value).one();
    }

    @Override
    public List<T> getListByProperty(SFunction<T,?> property, Object value){
        return lambdaQuery().eq(property,value).list();
    }

    @Override
    public T getOneByColumn(String column,Object value){
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        return getOne(queryWrapper.eq(column, value));
    }

    @Override
    public List<T> getListByColumn(String column,Object value){
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        return list(queryWrapper.eq(column, value));
    }


    public EqConditions<T> eqConditions(){
        return EqConditions.create();
    }

    @Override
    public Integer countByPropertyExcludeId(Integer id, SFunction<T ,?> property,Object value){
        LambdaQueryChainWrapper<T> lambdaQueryChainWrapper = lambdaQuery()
                .eq(property, value);
        List<T> list = lambdaQueryChainWrapper.list();
        if (id != null && id > 0) {
            list.removeIf(e -> e.getId().equals(id));
        }
        return list.size();
    }

    @Override
    public Integer countByProperty(SFunction<T ,?> property,Object value){
        LambdaQueryChainWrapper<T> lambdaQueryChainWrapper = lambdaQuery()
                .eq(property, value);
        return lambdaQueryChainWrapper.count();
    }

}
